package com.jmpt.fangbohui.utils;

import android.os.SystemClock;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zxx on 2015/8/4.
 */
public class TimeUtils
{

    public final static int TIME_TYPE_ALL = 1;
    public final static int TIME_TYPE_ONLY_HM = 2;

    /***
     * 获取当天最早的时间
     */
//    public static String getCurrentFirstTime()
//    {
////        Date curDate = new Date(System.currentTimeMillis());
//
//    }


    /**
     * 获取当前时间，格式yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentTime()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }


    /**
     * 获取格式化后的时间
     */
    public static String getTime(Date date)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static String getTimeIncludeHM(Date date)
    {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }


    /***
     * 时间设置是否合理（开始时间再结束时间之前）
     */
    public static boolean timeAvaJudgeMet(String startTime, String endTime, int type)
    {
        boolean isAva = true;
        if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime))
        {
            return isAva;
        }
        Calendar startCa = Calendar.getInstance();
        Calendar endCa = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (TIME_TYPE_ONLY_HM == type)
        {
            df = new SimpleDateFormat("HH:mm");
        }
        Date date = null;
        try
        {
            date = df.parse(startTime);
            startCa.setTime(date);
            date = df.parse(endTime);
            endCa.setTime(date);
            if (startCa.after(endCa))
            {
                isAva = false;
            }
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        return isAva;
    }

    public static String getCurrTime(){
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return df.format(new Date(SystemClock.currentThreadTimeMillis()));
    }

    /**
     * 将日期为2015 -1-2 转为化2015-01-02
     * @param whichForm
     * @return
     * @throws Exception
     */
    public static  String parseToStandardForm(String whichForm) throws Exception {
        if (TextUtils.isEmpty(whichForm)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timeStart = sdf.parse(whichForm).getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStart);
        System.out.println(timeStart + " = "
                + formatter.format(calendar.getTime()));
        return formatter.format(calendar.getTime());
    }
    
    public static String parseToDayOFWeek(long whichTime) throws Exception {
        String s = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(whichTime);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        s = year + "年" + (month + 1) + "月" + day + "日";

        int WeekOfYear = (calendar.get(Calendar.DAY_OF_WEEK) - 1);// 一周的第几天
        switch (WeekOfYear) {
            case 1:
                // terminalDate = "星期一 " + hour + ":" + minute;
                s = s + "  星期一";
                break;
            case 2:
                s = s + "  星期二";
                // terminalDate = "星期二 " + hour + ":" + minute;
                break;
            case 3:
                s = s + "  星期三";
                // terminalDate = "星期三 " + hour + ":" + minute;
                break;
            case 4:
                s = s + "  星期四";
                // terminalDate = "星期四 " + hour + ":" + minute;
                break;
            case 5:
                s = s + "  星期五";
                // terminalDate = "星期五 " + hour + ":" + minute;
                break;
            case 6:
                s = s + "  星期六";
                // terminalDate = "星期六 " + hour + ":" + minute;
                break;
            case 0:
                s = s + "  星期天";
                // terminalDate = "星期天 " + hour + ":" + minute;
                break;

            default:
                break;
        }
        return s;
    }
    
}
